﻿using System;
using System.Collections.Generic;

namespace Framework
{
    public sealed class LanguageHelper
    {
        public static string[] GetAllLangShortcut()
        {
            Type enumType = typeof(LanguageType);
            List<string> shortcuts = new List<string>();
            foreach (var item in Enum.GetValues(enumType))
            {
                shortcuts.Add(item.ToString());
            }

            return shortcuts.ToArray();
        }

        public static LanguageType[] GetAllLangEnums()
        {
            Type enumType = typeof(LanguageType);
            List<LanguageType> enums = new List<LanguageType>();
            foreach (var item in Enum.GetValues(enumType))
            {
                enums.Add((LanguageType)item);
            }

            return enums.ToArray();
        }

        public static bool IsLangIsDefined(string shortcut)
        {
            foreach (var item in GetAllLangShortcut())
            {
                if (item.Equals(shortcut))
                {
                    return true;
                }
            }
            return false;
        }

        public static string GetLangResRootDir(string shortcut)
        {
            return "lang/" + shortcut;
        }

        public static string GetLangShortcutFromAssetPath(string assetPath)
        {
            assetPath = FileHelper.GetUnityPath(assetPath);
            if (assetPath.Contains("/lang/"))
            {
                assetPath = assetPath.Replace("/lang/", "$");
                string[] splits = assetPath.Split('$');
                splits = splits[splits.Length - 1].Split('/');
                string shortcut = splits[0];
                if (IsLangIsDefined(shortcut))
                {
                    return shortcut;
                }
            }
            return "";
        }

        public static string LangTypeToShotcut(int langTypeInt)
        {
            LanguageType langType = (LanguageType)langTypeInt;
            if (Enum.IsDefined(typeof(LanguageType), langType))
            {
                return langType.ToString();
            }
            return LanguageType.zh.ToString();
        }

        public static int ShotcutToLangTypeInt(string shortcut)
        {
            LanguageType langType = (LanguageType)Enum.Parse(typeof(LanguageType), shortcut);
            if (Enum.IsDefined(typeof(LanguageType), langType))
            {
                return (int)langType;
            }
            Logger.LogWarning($"LanguageHelper.ShotcutToLangType, shortcut = {shortcut} is not defined, use zh instead!");
            return (int)LanguageType.zh;
        }
    }
}